package com.zw.pdm.module.est.service;

import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.est.CardDetail;
import com.zw.pdm.entity.est.Worktime;
import com.zw.pdm.module.basic.mapper.VerifyMapper;
import com.zw.pdm.module.est.mapper.CardDetailMapper;
import com.zw.pdm.module.est.mapper.CardMapper;
import com.zw.pdm.module.est.model.CardModel;
import com.zw.pdm.module.est.repository.CardDetailRepository;
import com.zw.pdm.module.est.repository.CardRepository;
import com.zw.pdm.module.est.repository.WorkTimeRepository;
import com.zw.pdm.module.report.mapper.VerifyProcessMapper;
import com.zw.pdm.module.report.repository.VerifyProcessRepository;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class WorkTimeService {

    @Autowired
    private CardRepository cardRepository;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardDetailMapper cardDetailMapper;
    @Autowired
    private CardDetailRepository cardDetailRepository;
    @Autowired
    private VerifyMapper verifyMapper;
    @Autowired
    private VerifyProcessRepository verifyProcessRepository;
    @Autowired
    private VerifyProcessMapper verifyProcessMapper;
    @Autowired
    private LogService logService;
    @Autowired
    private WorkTimeRepository workTimeRepository;

    /** 列表 */
    public List<CraMakeSheet> findWithWorkTime(CraMakeSheet craMakeSheet) {
        return cardMapper.findWithWorkTime(craMakeSheet);
    }

    /** 子列表查询 */
    public List<CardModel> findSlaveWithWorkTime(CardModel card) {
        return cardMapper.findSlaveWithWorkTime(card);
    }

    /** 按主键查询 */
    public Card getCardById(Integer id) {
        return cardRepository.getOne(id);
    }

    /** 查询工艺卡明细 */
    public List<CardDetail> findCardDetailByCardId(Integer id) {
        return cardDetailMapper.findByCardId(id);
    }

    /** 判断是否可以提交 */
    public boolean preCommit(Integer id) {
        int row = cardMapper.countCraMakeDetailBySheetId(id);
        int row2 = cardMapper.countIsQuotaCardByCraMakeSheetId(id);
        return row == row2;
    }

    /** 提交 */
    @Transactional(rollbackFor = Exception.class)
    public int commit(Integer id) {
        logService.saveLog("提交", "工艺制作件ID：" + id);
        cardMapper.updateCraMakeSheetCommitTime(id, new Date());
        cardMapper.updateCraMakeSheetCommitStatus(id, 3);
        cardMapper.updateCraMakeSheetChangeCommit(id, 1);
        cardMapper.updateCraMakeSheetChangeNotify(id, 0);

        /*List<Card> cards = cardMapper.findByMakeSheetId(id);
        // 查询审核配置
        Verify verify = new Verify();
        verify.setReportType(4);
        List<Verify> verifyList = verifyMapper.findByCondition(verify);

        for (Card card : cards) {
            if (card.getStatus() == 4) {
                break;
            }
            // 生成审核记录
            for (int i = 0; i < verifyList.size(); i++) {
                VerifyProcess vp = new VerifyProcess();
                vp.setCardId(card.getId());
                vp.setOrderNo(verifyList.get(i).getOrderNo());
                vp.setComp(verifyList.get(i).getComp());
                if (i == 0) {
                    vp.setCur(1);
                    vp.setStart(1);
                } else {
                    vp.setCur(0);
                    vp.setStart(0);
                }
                vp.setStatus(1);
                vp.setVerifier(verifyMapper.findVerifier(ShiroUtils.getUserId(), verifyList.get(i).getPost()));
                verifyProcessRepository.save(vp);
            }
        }*/
        cardMapper.deleteWorkTimeByMakeSheetId(id);
        List<Card> cards = cardMapper.findByMakeSheetId(id);
        for (Card card : cards) {
            // 保存工时，
            if (card.getIsQuota() == 1) {
                saveWorkTime(card.getId());
            }
        }
        cardMapper.updateCraSheetCommitTimeByCraMakeSheetId(id, new Date());
        return cardMapper.updateCraSheetStatusByCraMakeSheetIdOne(id, 4);
    }

    private void saveWorkTime(Integer id) {
        List<CardDetail> details2 = cardDetailMapper.findByCardId(id);
        for (CardDetail cardDetail : details2) {
            if (cardDetail.getWorkTimeOne() != null || cardDetail.getWorkTimeTwo() != null) {
                Worktime worktime = new Worktime();
                worktime.setCardId(id);
                worktime.setCardDetailId(cardDetail.getId());
                worktime.setIsDel(0);
                worktime.setProcess(cardDetail.getProcess());
                worktime.setEquName(cardDetail.getEquName());
                worktime.setWorkTimeOne(cardDetail.getWorkTimeOne());
                if (null==cardDetail.getWorkTimeTwo()){
                    worktime.setWorkTimeTwo(0.00);
                }else {
                    worktime.setWorkTimeTwo(aa(cardDetail.getWorkTimeTwo(),cardDetail.getDrawNum()));
                }
                workTimeRepository.save(worktime);
            }
        }
    }
    public static Double aa(double a, double b) {
        if (a==0){
            return  b;
        }
        if (b==0){
            return  a;
        }
        int aa = (int) a * 100 / 100;
        int bb = (int) b * 100 / 100;
        int cc = (int) (Math.round(a * 100) % 100) + (int) (b * 100 % 100);
        return Double.parseDouble(aa + bb + cc / 60 + "." + (cc % 60 >=10? cc % 60:("0"+cc % 60)));

    }
    public static Double aa(double a, int b) {
        int am = (int)(a *100);
        int aa = am / 100;
        int bb = am % 100;
        int tou = aa*b;
        int wei = bb*b;
        String cc = (tou+wei/60)+"."+(wei%60>=10? wei %60:("0"+wei%60));
        return Double.parseDouble(cc);
    }

    /** 撤消提交 */
    /*@Transactional(rollbackFor = Exception.class)
    public int cancelCommit(Integer id) {
        logService.saveLog("撤消转定额", "工艺卡ID：" + id);
        // 修改状态
        cardMapper.updateStatusById(id, 3);
        // 查询 makeSheet 是否还有已提交的工艺卡，如果没有，修改状态为未提交， 如果有修改为部分提交
        Integer makeSheetId = cardMapper.getMakeSheetIdByCardId(id);
        Integer count = cardMapper.countStatusByMakeSheetId(makeSheetId, 4);
        if (count > 0) {
            cardMapper.updateMakeSheetIsCommitById(makeSheetId, 2);
        } else {
            cardMapper.updateMakeSheetIsCommitById(makeSheetId, 1);
        }
        // 删除生成的审核记录
        return verifyProcessMapper.delByCardId(id);
    }*/

    /** 定额 */
    public int save(Card card, Integer[] detailId, Double[] workTimeOne, Double[] workTimeTwo) {
        for (int i = 0; i < detailId.length; i++) {
            CardDetail cardDetail = cardDetailRepository.getOne(detailId[i]);
            if (workTimeOne.length > 0) {
                cardDetail.setWorkTimeOne(workTimeOne[i]);
            }else {
                cardDetail.setWorkTimeOne(null);
            }
            if (workTimeTwo.length > 0) {
                cardDetail.setWorkTimeTwo(workTimeTwo[i]);
            } else {
                cardDetail.setWorkTimeTwo(null);
            }
            cardDetailRepository.saveAndFlush(cardDetail);
        }
        Card target = cardRepository.getOne(card.getId());
        target.setIsQuota(1);
        target.setQuotaCode(ShiroUtils.getLoginName());
        target.setQuotaName(ShiroUtils.getUser().getName());
        target.setQuotaBranchName(ShiroUtils.getUser().getBranchName());
        target.setQuotaTime(new Date());
        cardRepository.saveAndFlush(target);
        logService.saveLog("工艺卡工时定额", "工艺卡ID：" + card.getId());
        return 1;
    }

    /** 不需要定额 */
    public int need(Integer id) {
        cardDetailMapper.updateWorkTimeByCardId(id);
        return cardMapper.updateIsQuotaById(id, 2);
    }

    /** 驳回 */
    public int reject(Integer id) {
        //cardDetailMapper.updateWorkTimeByCardId(id);
        cardMapper.updateIsQuotaById(id, 0);
        cardMapper.updateIsCheckAndVerifyTimeById(id, 0, null);
        cardMapper.updateStatusById(id, 1);
        return cardMapper.delCraftRouteByCardId(id);
    }
}
